package fr.ufc.kiestla;

import java.util.Date;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;

public class SeleniumTool{
	public static final String baseURL = "https://disc.univ-fcomte.fr/master2/";
	public static final Connexion adminId = new Connexion("admin","admin");
	
	private static void rempliCreation(WebDriver driver, Connexion identifiants){
	    driver.findElement(By.id("login")).clear();
	    driver.findElement(By.name("login")).sendKeys( identifiants.getLogin());
	    driver.findElement(By.id("mdp")).clear();
	    driver.findElement(By.name("mdp")).sendKeys( identifiants.getMdp());
	}
	
	public static void createUser(WebDriver driver, TypeUser type, Connexion identifiants ){
	    connect(driver, new Connexion("admin","admin"));
	    driver.findElement(By.linkText("Ajout utilisateur")).click();
	    rempliCreation(driver, identifiants);
	    switch( type ){
	    case ETUDIANT:
		    driver.findElement(By.xpath("//label[1]")).click();
		    break;
	    case ENSEIGNANT:
		    driver.findElement(By.xpath("//label[2]")).click();
		    break;
	    case ADMINISTRATIF:
		    driver.findElement(By.xpath("//label[3]")).click();
		    break;
	    }
	    driver.findElement(By.cssSelector("button.btn.btn-default")).click();
	}
	
	public static Connexion createUser(WebDriver driver, TypeUser type ){
		Date now = new Date();
		Connexion identifiants = new Connexion("login"+now.getTime(), "mdp"+now.getTime());
		createUser(driver, type, identifiants);
	    return identifiants;
	}
	
	public static void deleteUser( WebDriver driver, Connexion user ){
		connect(driver, new Connexion("admin","admin"));
	    driver.findElement(By.linkText("Suppresion utilisateur")).click();
		driver.findElement(By.xpath("//td[text()='"+user.getLogin()+"']/following-sibling::td[2]/a")).click();
	}
	
	public static void connect(WebDriver driver, Connexion connexion ){
		driver.get(baseURL+"~groupe3/index.php");
		driver.findElement(By.name("login")).clear();
	    driver.findElement(By.name("login")).sendKeys(connexion.getLogin());
	    driver.findElement(By.name("pass")).clear();
	    driver.findElement(By.name("pass")).sendKeys(connexion.getMdp());
	    driver.findElement(By.xpath("//button[@type='submit']")).click();
	}
	
	public static Module createModule(WebDriver driver, String referant ){
		Module m = new Module("module"+new Date().getTime(),referant);
		createModule(driver, m );
		return m;
	}
	public static void createModule(WebDriver driver, Module module){
		connect(driver, new Connexion("admin","admin"));
	    driver.findElement(By.linkText("Ajout module")).click();
	    fillModuleFormulaire(driver, module);
	    driver.findElement(By.cssSelector("button.btn.btn-default")).click();
	}
	
	public static void fillModuleFormulaire( WebDriver driver, Module module ){
	    driver.findElement(By.id("nom")).clear();
	    driver.findElement(By.id("nom")).sendKeys(module.getNom());
	    new Select(driver.findElement(By.name("ensref"))).selectByVisibleText(module.getReferant());
	}
	
	public static void deleteModule( WebDriver driver, Module module){
		connect(driver, adminId);
	    driver.findElement(By.linkText("Supression module")).click();
	    driver.findElement(By.xpath("//td[text()=\""+module.getNom()+"\"]/following-sibling::td[2]/a")).click();
	}
	
	public static void modifierModule(WebDriver driver, Module old_module, Module new_module ){
		connect(driver, adminId);
	    driver.findElement(By.linkText("Modification module")).click();
	    driver.findElement(By.xpath("//tr/td[text()=\""+old_module.getNom()+"\"]/following-sibling::td[2]/button")).click();
	    driver.findElement(By.xpath("//button[@id=\""+old_module.getNom()+"\"]/../..//input[@id=\"nom\"]")).clear();
	    driver.findElement(By.xpath("//button[@id=\""+old_module.getNom()+"\"]/../..//input[@id=\"nom\"]")).sendKeys(new_module.getNom());
	    driver.findElement(By.xpath("//button[@id=\""+old_module.getNom()+"\"]")).click();
	}
		
	public static void signalerEtudiant(WebDriver driver, Signalement signalement ){
		driver.get(baseURL+"~groupe3/accueil_enseignant.php");
		driver.findElement(By.xpath("//tr/td[2][text()=\""+signalement.getModule().getNom()+"\"]/following-sibling::td[1]/a")).click();
		new Select(driver.findElement(By.xpath("//select"))).selectByVisibleText(signalement.getEtudiant().getLogin());
		driver.findElement(By.xpath("id('datepicker')")).clear();
		driver.findElement(By.xpath("id('datepicker')")).sendKeys(signalement.getDate());
		driver.findElement(By.xpath("//textarea")).clear();
		driver.findElement(By.xpath("//textarea")).sendKeys(signalement.getCommentaire());
	    driver.findElement(By.xpath("//button[@class=\"btn btn-primary\"]")).click();
	}
	
	public static void ajouterEtudiantModule(WebDriver driver, Module module, Connexion etudiant){
		driver.get(baseURL+"~groupe3/accueil_enseignant.php");
		driver.findElement(By.xpath("//tr/td[text()=\""+module.getNom()+"\"]/following-sibling::td[2]/a")).click();
		new Select(driver.findElement(By.xpath("//select"))).selectByVisibleText(etudiant.getLogin());
		driver.findElement(By.xpath("//button[@type=\"submit\"]")).click();
	}
}
